package com.tender.service.impl;

import cn.hutool.core.lang.Assert;
import com.tender.domain.ShiroUser;
import com.tender.domain.ShiroUserCondition;
import com.tender.domain.ShiroUserExample;
import com.tender.mapper.ShiroUserMapper;
import com.tender.service.ShiroUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

import com.tender.utils.DataUtils;
import org.springframework.util.CollectionUtils;

@Service
public class ShiroUserServiceImpl implements ShiroUserService {

    @Autowired
    private ShiroUserMapper shiroUserMapper;

    @Override
    public ShiroUser selectOne(ShiroUserCondition param) {
        // Assert.notNull(param.getRowId(), "用户rowId不能为空");
        ShiroUserExample example = new ShiroUserExample();
        ShiroUserExample.Criteria criteria = example.createCriteria();

        DataUtils.setLongValueIfNotNull(param.getRowId(), criteria::andRowIdEqualTo);
        DataUtils.setValueIfNotBlank(param.getLoginName(), criteria::andLoginNameEqualTo);
        List<ShiroUser> shiroUsers = shiroUserMapper.selectByExample(example);

        return CollectionUtils.isEmpty(shiroUsers) ? null : shiroUsers.get(0);
    }

    @Override
    public List<ShiroUser> selectList(ShiroUserCondition param) {
        ShiroUserExample example = new ShiroUserExample();
        ShiroUserExample.Criteria criteria = example.createCriteria();

        DataUtils.setValueIfNotBlank(param.getLoginName(), criteria::andLoginNameEqualTo);
        DataUtils.setValueIfNotBlank(param.getLoginNameLike(), loginNameLike -> criteria.andLoginNameLike(DataUtils.likeIn(loginNameLike)));

        List<ShiroUser> shiroUsers = shiroUserMapper.selectByExample(example);
        return CollectionUtils.isEmpty(shiroUsers) ? new ArrayList<>() : shiroUsers;
    }

    @Override
    public int updateByCondition(ShiroUserCondition param) {
        return 0;
    }

    @Override
    public int updateByPrimaryKey(ShiroUserCondition param) {
        return 0;
    }

    @Override
    public int insertSelective(ShiroUser param) {
        return shiroUserMapper.insertSelective(param);
    }

    @Override
    public int insertListSelective(List<ShiroUser> params) {
        return 0;
    }

    @Override
    public int deleteByCondition(ShiroUserCondition param) {
        return 0;
    }

    @Override
    public int deleteByPrimaryKey(Long rowId) {
        Assert.notNull(rowId, "用户rowId不能为空");
        ShiroUserExample example = new ShiroUserExample();
        ShiroUserExample.Criteria criteria = example.createCriteria();
        criteria.andRowIdEqualTo(rowId);

        return shiroUserMapper.deleteByExample(example);
    }
}
